package org.admin.controller;

import core.Data;
import core.data.DataResult;
import org.admin.entity.dto.PermsDto;
import org.admin.service.PermsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
@RestController
@RequestMapping("/admin/perms")
public class PermsController {
    @Autowired
    private PermsService service;
    @GetMapping("/query")
    public DataResult query(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<PermsDto>addHandle("params", data -> {
                    new PermsDto().validate(new String[] {
                        "appId", "name", "route", "method", "power", "groupId", "*current", "size"
                    }).check(data);
                }).addHandle("query", service::query)
                .start().getResult();
    }

    @GetMapping("/fetch")
    public DataResult fetch(@RequestParam Map<String, Object> get){
        return Data.create(get).start().getResult();
    }

    @PostMapping("/create")
    public DataResult create (@RequestBody Map<String, Object> post){
        return Data.create(post)
                .<PermsDto>addHandle("params", data -> {
                    new PermsDto().validate(new String[] {
                        "id", "*appId", "*name", "*route", "*method", "*power", "detail", "*groupId"
                    }).check(data);
                }).addHandle("create", service::create)
                .start().getResult();
    }

    @GetMapping("/info")
    public DataResult info(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<PermsDto>addHandle("params", data -> {
                    new PermsDto().validate(new String[] {
                        "*id"
                    }).check(data);
                }).addHandle("create", service::info)
                .start().getResult();

    }

    @PostMapping("/remove")
    public DataResult remove(@RequestBody Map<String, Object> post){
        return Data.create(post)
                .<PermsDto>addHandle("params", data -> {
                    new PermsDto().validate(new String[] {
                         "ids"
                    }).check(data);
                }).addHandle("create", service::remove)
                .start().getResult();
    }

}
